/* Name: hidtool.c
 * Project: hid-data example
 * Author: Christian Starkjohann
 * Creation Date: 2008-04-11
 * Tabsize: 4
 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
 * This Revision: $Id: hidtool.c 723 2009-03-16 19:04:32Z cs $
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hiddata.h"
#include "../firmware/usbconfig.h"  /* for device VID, PID, vendor name and product name */

/* ------------------------------------------------------------------------- */

static char *usbErrorMessage(int errCode)
{
static char buffer[80];

    switch(errCode){
        case USBOPEN_ERR_ACCESS:      return "Access to device denied";
        case USBOPEN_ERR_NOTFOUND:    return "The specified device was not found";
        case USBOPEN_ERR_IO:          return "Communication error with device";
        default:
            sprintf(buffer, "Unknown USB error %d", errCode);
            return buffer;
    }
    return NULL;    /* not reached */
}

static usbDevice_t  *openDevice(void)
{
usbDevice_t     *dev = NULL;
unsigned char   rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char            vendorName[] = {USB_CFG_VENDOR_NAME, 0}, productName[] = {USB_CFG_DEVICE_NAME, 0};
int             vid = rawVid[0] + 256 * rawVid[1];
int             pid = rawPid[0] + 256 * rawPid[1];
int             err;

    if((err = usbhidOpenDevice(&dev, vid, vendorName, pid, productName, 0)) != 0)
	{
        fprintf(stderr, "error finding %s: %s\n", productName, usbErrorMessage(err));
        return NULL;
    }
    return dev;
}

/* ------------------------------------------------------------------------- */

static void hexdump(char *buffer, int len)
{
	int     i;
	FILE    *fp = stdout;

    for(i = 0; i < len; i++){
        if(i != 0){
            if(i % 16 == 0){
                fprintf(fp, "\n");
            }else{
                fprintf(fp, " ");
            }
        }
        fprintf(fp, "0x%02x", buffer[i] & 0xff);
    }
    if(i != 0)
        fprintf(fp, "\n");
}

/* ------------------------------------------------------------------------- */

static void usage(char *myName)
{
    fprintf(stderr, "usage:\n");
    fprintf(stderr, "  %s led1\n", myName);
	fprintf(stderr, "  %s led2\n", myName);
	fprintf(stderr, "  %s rled1\n", myName);
	fprintf(stderr, "  %s rled2\n", myName);
	fprintf(stderr, "  %s version\n", myName);
}

int main(int argc, char **argv)
{
	usbDevice_t *dev;
	char        buffer[129];    /* room for dummy report ID */
	int         err;

    if(argc < 2)
	{
        usage(argv[0]);
        exit(1);
    }
    
	if((dev = openDevice()) == NULL)
        exit(1);

    if(strcasecmp(argv[1], "led1") == 0)
	{
        memset(buffer, 0, sizeof(buffer));
		buffer[0] = 1; /* use ReportID 1 */
        if ((err = usbhidSetReport(dev, buffer, 3)) != 0) 
            fprintf(stderr, "error writing data: %s\n", usbErrorMessage(err));
    }
	else if(strcasecmp(argv[1], "led2") == 0)
	{
        memset(buffer, 0, sizeof(buffer));
		buffer[0] = 2; /* use ReportID 2*/
        if ((err = usbhidSetReport(dev, buffer, 3)) != 0)
            fprintf(stderr, "error writing data: %s\n", usbErrorMessage(err));
	}
	else if(strcasecmp(argv[1], "rled1") == 0)
	{
        memset(buffer, 0, sizeof(buffer));
		int len = 2;
        if((err = usbhidGetReport(dev, 1, buffer, &len)) != 0) /* ReportID 1*/
		{ 
            fprintf(stderr, "error reading data: %s\n", usbErrorMessage(err));	
		}
		else
		{
			//hexdump(buffer, len);
			buffer[1] == 1 ? printf("LED1 an\n") : printf("LED1 aus\n");
		}
	}
	else if(strcasecmp(argv[1], "rled2") == 0)
	{
        memset(buffer, 0, sizeof(buffer));
		int len = 2;
        if((err = usbhidGetReport(dev, 2, buffer, &len)) != 0) /* ReportID 2*/
		{ 
            fprintf(stderr, "error reading data: %s\n", usbErrorMessage(err));	
		}
		else
		{
			//hexdump(buffer, len);
			buffer[1] == 1 ? printf("LED2 an\n") : printf("LED2 aus\n");
		}
	}
	else if(strcasecmp(argv[1], "version") == 0)
	{
        memset(buffer, 0, sizeof(buffer));
		int len = 11;
		char out[11];
		memset (out, 0, sizeof(out));
        if((err = usbhidGetReport(dev, 3, buffer, &len)) != 0) /* ReportID 3*/
		{ 
            fprintf(stderr, "error reading data: %s\n", usbErrorMessage(err));	
		}
		else
		{
			//hexdump(buffer, len);
			memcpy(out, &buffer[1], sizeof(buffer) -1);
			fprintf(stderr, "UsbDeviceVersion: %s\n", out);
		}
	}	
	else
	{
        usage(argv[0]);
        exit(1);
    }
    usbhidCloseDevice(dev);
    return 0;
}

/* ------------------------------------------------------------------------- */
